package drive.Service.ServiceFormat;

import com.google.gson.Gson;
import drive.Mapper.FollowRelationMapper;
import drive.Model.FollowRelation;
import drive.Service.ServiceInterface.FollowRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FollowRelationServiceA implements FollowRelationService {
    @Autowired
    private FollowRelationMapper followRelationMapper;

    private Gson gson = new Gson();

    @Override
    public String findAll() {
        List<FollowRelation> list = followRelationMapper.findAll();
        return gson.toJson(list);
    }

    @Override
    public String findByFollowerAndFollowed(int followerID, int followedID) {
        FollowRelation followRelation = followRelationMapper.findByFollowerAndFollowed(followerID, followedID);
        return gson.toJson(followRelation);
    }

    @Override
    public String findByFollowerID(int followerID) {
        List<FollowRelation> list = followRelationMapper.findByFollowerID(followerID);
        return gson.toJson(list);
    }

    @Override
    public String findByFollowedID(int followedID) {
        List<FollowRelation> list = followRelationMapper.findByFollowedID(followedID);
        return gson.toJson(list);
    }

    @Override
    public String add(FollowRelation followRelation) {
        FollowRelation check = followRelationMapper.findByFollowerAndFollowed(followRelation.getFollowerID(),followRelation.getFollowedID());
        if(check == null){
            int result = followRelationMapper.add(followRelation);
            return "关注成功";
        }
        return "已关注";
    }

    @Override
    public String update(FollowRelation followRelation) {
        int result = followRelationMapper.update(followRelation);
        return gson.toJson(result);
    }

    @Override
    public String delete(int followerID, int followedID) {
        int result = followRelationMapper.delete(followerID, followedID);
        return "取消关注成功";
    }
}